home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Diamond Collection
/
The Diamond Collection (Software Vault)(Digital Impact).ISO
/
cdr44
/
pctim12.zip
/
PCTIMER.DOC
< prev
next >
Wrap
Text File
|
1995-01-29
|
9KB
|
206 lines
--------------------------- PCTIMER 1.2 --------------------------
Program written by: Chih-Hao Tsai (c-tsai4@uiuc.edu) 1/29/95
------------------------------------------------------------------
This program is FREE. Feel free to use, re-distribute or modify
it. However, you should recognize that this program comes without
any warranty. It is recommended that you examine and test this
program before actually use it. If you find this program useful,
or if you find a bug, please send an e-mail to me to let me know.
------------------------------------------------------------------
1. PCTIMER
PCTIMER is a high-resolution timing routine in the form of C
functions. You can compile and link PCTIMER to your programs in
which millisecond (or even higher) accuracy timing is required.
History
11/5/94 Version 1.0
The first freeware release of PCTIMER. With source codes for
GNU C/C++ and Microsoft C/C++ compilers.
1/16/95 Version 1.1
(1) In version 1.0, gccint8.c only intercepts protected mode
int 8h, which may cause loss of ticks when programs switch to
real mode. I revised gccint8.c that it would intercept both
protected mode and real mode int8h's. (I would like to thank DJ
Delorie, Eli Zaretskii, and Long Doan for their helpful
suggestions and discussions.)
(2) I added sources for Turbo C/C++ compiler.
(3) The delay8h function had been slightly modified.
1/29/95 Version 1.2
Revision (and correction) of gccint8.c. PCTIMER for GCC was
first developed under gcc 2.6.0, and it was found that the
following instructions: "r.x.cs = rm_old_handler.rm_segment;
r.x.ip = rm_old_handler.rm_offset; r.x.ss = r.x.sp = 0;
_go32_dpmi_simulate_fcall_iret(&r);" could not call
rm_old_handler.rm_segment:rm_old_handler.rm_offset correctly.
Therefore, my solution in PCTIMER 1.1 was to call f000:fea5,
which is the default handler of int 8h in IBM PC and fully
compatible BIOSes. Since the bug has been fixed in gcc 2.6.3,
the real mode interrupt handler "rm_new8h" in gccint8.c could
now be revised. As a result, PCTIMER for GCC now requires GCC
version 2.6.3 or higher.
2. RATIONALE FOR WRITING AND USING PCTIMER
Although the timing routines of most programming languages
always report times to centisecond, the resolution of standard
timer of IBM-PCs is actually only 1/18.2 seconds. That is, if
you use the standard timing routine provided by your
programming language, the precision of your measurement is
actually 55 milliseconds with a plus/minus 27.5 seconds of
measurement error! I wrote PCTIMER to provide IBM-PCs with
high-resolution timing ability.
3. FEATURES
(1) Precise: Basically, PCTIMER is designed to provide a timing
routine with millisecond resolution. If such a resolution does
not satisfy you, you can also modify it to get higher
resolutions. However, in most cases, millisecond resolution is
precise enough.
(2) Stable: PCTIMER sets new parameters to the 8254 IC
(Programmable Interrupt Timer) to get high frequencies of
SYSTEM TIMER Interrupt (IRQ0, or INT8h) whose default is 18.2
times per second. However, many hardware routines depend on
IRQ0. Abnormal IRQ0 frequencies may cause the system unstable.
To maintain the PC's stability, PCTIMER intercepts the INT8,
only calls the original INT8 at the frequency of 18.2 times per
second. Consequently, high-resolution timing and system
stability are simultaneously achieved.
(3) Flexible: Originally I wrote PCTIMER to do millisecond
resolution timing. If millisecond precision in unnecessary to
you, you can also set PCTIMER to do centisecond timing.
Resolution higher than millisecond is also possible, but you
have to modify a few parts of the program by yourself. It
should be an easy task.
(4) Portable: Most public-domain timing routines are written in
assembly codes, or in C codes with in-line assembly codes. The
assembly codes make those routine difficult to understand and
use if one does not have good background of the assembly
language. And the format of in-line assembly varies from one
compiler to another. Worst of all, several C compilers do not
support in-line assembly. PCTIMER does not have these problems,
because all functions are written in C codes. There are no
assembly codes at all.
4. SYSTEM AND COMPILER REQUIREMENTS
(1) PCTIMER is a tiny timing routine that does not have
specific memory and CPU speed limitations. The only requirement
is that it only runs on IBM-AT compatible PCs. An XT is not
appropriate because an XT does not have an CMOS IC. PCTIMER
need the CMOS IC to obtain correct system time.
(2) PCTIMER comes with three versions, one for GNU C/C++, one
for Microsoft C/C++, and one for Turbo C/C++. The GCC version
was developed under GCC 2.6.3, the MSC version was developed
under MSC 6.0, and the Turbo C version was developed under
Turbo C/C++ 2.0. There should be no problems if you are using a
different version of Turbo C or MSC. However, the version of
GCC *does* matter. Please do not use GCC older than 2.6.3.
5. USING PCTIMER
(1) At the beginning of your program, include the header file
of PCTIMER:
#include "gccint8.h"
/* "mscint8.h" for MSC; "tcint8.h" for Turbo C */
(2) Declare three unsigned long integers (of course, their
names are arbitrary):
unsigned long int tick_1, tick_2, elapsedtime;
(3) In your program, use the following function and parameter
to initiate the timer:
init8h(1000); /* init8h(100) for centisecond resolution */
ticks_8h = 0;
(4) Timing
tick_1 = ticks_8h;
/* .... do some stuff here .... */
tick_2 = ticks_8h;
elapsedtime = time8h(tick_1, tick_2);
/* The function "time8h" always returns millisecond unit
digits. That is, if you use init8h(1000), then the value
returned by time8h is exactly the time elapsed between
tick_1 and tick_2. However, if you use init8h(100), the
value returned by time8h is still in the unit of
millisecond. The difference is that the value returned by
time8h in the init8h(1000) condition is measured in the
precision of one millisecond, while the value returned by
time8h in the init8h(100) condition is measured in the
precision of 10 milliseconds. */
(5) How to turn off the timer:
quit8h();
/* Be sure to turn off the timer before your program
terminates. Also turn off the timer before you set a new
parameter for init8h. */
(6) delay8h and sound8h
/* The "delay8h" is a high-resolution delay function. The
parameter is always in the unit of millisecond, no matter
what you put in the "init8h". However, the precision of the
delay duration depends on the parameter of "init8h". Here
is an example for a 150 milliseconds delay: */
delay8h(150);
/* the "sound8h" takes advantage of "delay8h" to generate a
sound with a specific duration. Here is an example for a
800 Hz sound with 200 ms duration: */
sound(800, 200);
6. LIMITATION
PCTIMER uses an unsigned long integer to count the cumulative
frequency of INT8h interrupts. Assuming you set init8h(1000),
ticks_8h = 0, PCTIMER will work 4294967296/1000 = 4294967.296
seconds, or 1193.05 hours without encountering the problem of
overflow. I hope that the 1193.05 hours would be pretty enough.
If you do need to continuously use PCTIMER for a period longer
than 1193.05 hours, just reset the ticks_8h to zero at
appropriate location of your program.
7. FILES
pctimer.doc This file
gccint8.h PCTIMER: Header file for GCC
gccint8.c PCTIMER: Source code for GCC
mscint8.h PCTIMER: Header file for Microsoft C
mscint8.c PCTIMER: Source code for Microsoft C
tcint8.h PCTIMER: Header file for Turbo C
tcint8.c PCTIMER: Source code for Turbo C
test8.c Source code for test8.exe
test8.exe Test program (Note 1)
maketest.bat Simple batch file to create test8.exe (Note 2)
Note 1. This test program was compiled by GCC 2.6.3. That is,
it requires PCs with 32-bit CPUs and go32.exe.
Note 2. This batch file is also for GCC. Please make necessary
changes if you do not use GCC.
------------------------------------------------------------------